package com.browsesoft.user;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.LinkedList;

import com.browsesoft.Manager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.dbtools.DBTools;

public class FunctionManager implements Serializable, Manager {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1832399169304060674L;

	/**
	 * 根功能
	 */
	private LinkedList rootFunctions = new LinkedList();

	/**
	 * 管理器拥有的所有功能列表
	 */
	private LinkedList functions = new LinkedList();

	/**
	 * 功能ID和名称
	 */
	private String[][] functionFromDB = null;

	/**
	 * 功能和角色对应关系
	 */
	private String[][] functionWithRole = null;

	/**
	 * 装载功能
	 * 
	 * @throws Exception
	 */
	public void load() throws Exception {
		functionFromDB = DBTools
				.executeQueryWithTableHead("select * from t_function");
		functionWithRole = DBTools
				.executeQueryWithTableHead("select * from t_function_role");
		// 产生根
		rootFunctions = loadChildFunction("0");
		for (int i = 0; i < rootFunctions.size(); i++) {
			loadFunctions((Function) rootFunctions.get(i));
		}
	}

	/**
	 * 装载所有功能
	 */
	private void loadFunctions(Function function) throws Exception {
		// 将功能加入到功能列表和管理
		functions.add(function);
		// 根据该功能的ID号装载该功能下的所有子功能
		LinkedList v = loadChildFunction(function.getID());
		for (int i = 0; i < v.size(); i++) {
			try {
				// 生成新功能
				Function child = (Function) v.get(i);
				// 设置功能的父功能
				child.setParent(function);
				function.childFunctions.add(child);
				loadFunctions(child);
			} catch (Exception e) {
				System.out.println("装载功能出错");
			}
		}
	}

	/**
	 * 取得功能ID为给定ID下的所有子功能
	 * 
	 * @param ID
	 *            给定的功能ID，根功能ID为0
	 * @return 该功能下的所有子功能
	 */
	private LinkedList loadChildFunction(String ID) throws Exception {
		LinkedList v = new LinkedList();
		for (int i = 1; i < functionFromDB.length; i++) {
			try {
				// 得到父组织ID
				int column = DBTools.indexOf(functionFromDB[0], "parentid");
				String parentid = functionFromDB[i][column];
				// 为0或者为空，表示为根
				if (parentid.equals(ID)) {
					Function function = new Function(DBTools.getAttributes(
							functionFromDB, i));
					v.add(function);
					// 将功能加入到角色中
					for (int j = 1; j < functionWithRole.length; j++) {
						// 功能所对应的角色ID
						int col = DBTools.indexOf(functionWithRole[0],
								"function_id");
						String roleFunctionID = functionWithRole[j][col];
						col = DBTools.indexOf(functionWithRole[0], "role_id");
						String roleID = functionWithRole[j][col];
						// 相等，取得功能
						if (((String) function.getAttributes().get("id"))
								.equals(roleFunctionID)) {
							Role role = (Role) ManagerGroup.getManagerGroup()
									.getObject(roleID);
							function.add(role);
							role.add(function);
						}
					}
				}
			} catch (Exception e) {
				System.out.println("装载功能出错");
			}
		}
		return v;
	}

	/**
	 * 取得根功能
	 */
	public LinkedList getRootFunctions() {
		for (int i = 0; i < rootFunctions.size(); i++) {
			Function fun = (Function) rootFunctions.get(i);
			if (fun.getAttributes().get("location") == null) {
				fun.getAttributes().put("location", Integer.toString(i));
			}
		}
		return rootFunctions;
	}

	/**
	 * 取的所有功能
	 */
	public LinkedList getFunctions() {
		return functions;
	}

	/**
	 * 根据id取功能
	 * 
	 * @param id
	 * @return 功能
	 */
	public Object getObject(String id) {
		for (int i = 0; i < functions.size(); i++) {
			Function o = (Function) functions.get(i);
			if (o.getID().equals(id)) {
				return o;
			}
		}
		return null;
	}

	public void synchronAdd(String className, Hashtable attributes) {
		// TODO Auto-generated method stub

	}

	public void synchronRemove(String id) {
		// TODO Auto-generated method stub

	}

	public void synchronUpdate(String id, Hashtable attributes) {
		// TODO Auto-generated method stub

	}
}